#include "prot.h"
#include <stdio.h>

int main (int argc, char **argv)
{
	key_t key;
	int msg_id;
	struct msg_st rcv_buf;
	ssize_t size;

	//生成键值
	key = ftok(PATHNAME, PROJ_ID);
	if (-1 == key) {
		perror ("ftok()");
		return -1;
	}

	//创建/获取消息队列的实例
	msg_id = msgget (key, IPC_CREAT | IPC_EXCL | 0600);
	if (-1 == msg_id) {
		if (errno == EEXIST) {
			msg_id = msgget (key, 0);
		}else {
			perror ("msgget()");
			exit (1);
		}
	}
	//出队, 从消息队列中读消息
	while (1) {
		size = msgrcv (msg_id, &rcv_buf, MSGQUEUE_SIZE, 0, 0);
		if (-1 == size) {
			perror ("msgrcv()");
			exit (1);
		}
		printf ("-------------接受成功--------------\n");
		printf ("msg:%s\n", rcv_buf.msg);
		printf ("-------------接受完毕--------------\n");
	}
	return 0;
}
